package com.frxuexi.mdm2201

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch

class FlowActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow)

        val flow = flowOf(1, 2, 3, 4,5,6,7)
        lifecycleScope.launch {
            flow.flowOn(Dispatchers.IO)
                .catch {
                    it.printStackTrace()
                }
                .filter {
                    it%2==0
                }
                .drop(1)
//                .take(4)
                .map {
                    it*2
                }
                .collect(::println)//俱名函数
        }

//        {myMsg()}.asFlow()
        kotlinx.coroutines.flow.flow<Int> {1  }


        /*lifecycleScope.launch {
            myFlow2().collect{
                Log.i("===",it.toString())
            }
        }*/



    }

    fun myFlow():Flow<Int>{
        return arrayOf(1, 2, 3).asFlow()
    }
    fun myFlow2():Flow<Int>{
       return flow<Int> {
           print("aaa")
            emit(2)


        }

    }


    fun myMsg():Int{
        return 12
    }
}